package island.gotenyama.wand.bean;

import island.gotenyama.wand.annotation.FieldDefinition;
import island.gotenyama.wand.enums.JavaFieldType;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
 * 結果行を表すクラス
 * 
 * @author tk
 */
public class DataRow {

	private Map<String, Object> dataRow = new HashMap<String, Object>();

	@SuppressWarnings("unchecked")
	public <T> T get(AField aField) {
		FieldDefinition def = aField.getFieldDefinition();
		return (T) dataRow.get(def.name());
	}

	@SuppressWarnings("unchecked")
	public <T> T get(String name) {
		return (T) dataRow.get(name);
	}

	public <T> void put(AField aField, ResultSet rs) throws SQLException {
		FieldDefinition def = aField.getFieldDefinition();
		JavaFieldType javaType = def.typeJava();
		Object result = null;
		switch (javaType) {
			case STRING:
				result = rs.getString(def.name());
				break;
			case INTEGER:
				result = rs.getInt(def.name());
				break;
			case LONG:
				result = rs.getLong(def.name());
				break;
			case DOUBLE:
				result = rs.getDouble(def.name());
				break;
			case DATE:
				result = rs.getDate(def.name());
				break;
			default:
				result = rs.getObject(def.name());
				break;
		}
		this.dataRow.put(def.name(), result);
	}
}
